#include "map_tile.h"

TileMap::TileMap(boost::shared_ptr<btDynamicsWorld> ownerWorld)
  : Map( TILEMAP, ownerWorld )
{
	sectorsize = 150.0;
	hsectorsize = 0.5 * sectorsize;
	wallwidth = 2;
	hwallwidth = wallwidth/2;
	
	relativeY = 0.5 * sectorsize;
	hrelativeY = relativeY/2;
	
	hypotenuse = sqrt(((sectorsize+hwallwidth)*(sectorsize+hwallwidth))+(relativeY*relativeY));
	hhypotenuse = hypotenuse / 2;

	localScale = btVector3( hsectorsize, hrelativeY, hsectorsize );

// 	map_homedir = Dirlayout::Instance()->getHomedir();
// #if !defined(WIN32)
// 	map_homedir.append("/.stuntcoureur/maps/");
// #else
// 	map_homedir.append("maps/");
// #endif
// 	map_sysdir = Settings::Instance()->binarypath;
// #if !defined(WIN32)
// 	map_sysdir.append("../share/stuntcoureur/maps/");
// #else
// 	map_sysdir.append("stuntcoureur/maps/");
// #endif
}

void TileMap::setDimensions( unsigned int mwidth, unsigned int mheight )
{
	X = mwidth;
	Y = mheight;

	worldX = sectorsize * X;
	hworldX = worldX / 2;

	worldZ = sectorsize * Y;
	hworldZ = worldZ / 2;
	
// 	margin = sectorsize/20;
}

void TileMap::createBaseFloor()
{
	btVector3 localInertia = btVector3( 0.0f, 0.0f, 0.0f );
	
	btTransform transform; transform.setIdentity(); 

// 	OBJloader k("share/stuntcoureur/modules/topplate_A.obj");
// 	
// 	// FLOOR TILES
// 	for ( unsigned int i = 0; i < X; i++ )
// 	{
// 		for ( unsigned int j = 0; j < Y; j++ )
// 		{
// 			Color flcolor( (1.0f/(i+j+1))*j, 0.0f + (0.05*i), 1.0f - (0.05*i), 1.0f );
// 			flcolor.normalize();
// // 			btVector3 orig = getSectorVector(i, j, -hwallwidth);
// 
// // 			poffset.setOrigin( btVector3( 0, -hrelativeY, 0 )  );
// 
// 			transform.setOrigin( getSectorVector(i, j, -relativeY) );
// 
// // 			BodypartRigidBox* b = body.addBodyPart_Rigid_Box(
// // 				btVector3( hsectorsize, hwallwidth, hsectorsize ),	// dimensions
// // 				0, localInertia, transform, flcolor, 0, 0 );
// 
// 			BodypartRigidTrianglemesh* b = body.addBodyPart_Rigid_Trianglemesh(
// 				&k, 
// 				localScale, 0, localInertia, transform, flcolor, 0, 0 );
// 
// 			b->body->setFriction(1.0f);
// 			b->body->setRestitution(10.00f);
// 		}
// 	}

	// WORLD WALLS
	transform.setOrigin( btVector3( -hwallwidth, 50, hworldZ ) );
	body.addBodyPart_Rigid_Box( btVector3( hwallwidth, 50, hworldZ ), 0, localInertia, transform, BeColor( 0.5f, 1.0f, 1.0f, 0.0f ), 1.0f, 1.0f );

	transform.setOrigin( btVector3( worldX+hwallwidth, 50, hworldZ ) );
	body.addBodyPart_Rigid_Box( btVector3( hwallwidth, 50, hworldZ ), 0, localInertia, transform, BeColor( 0.5f, 1.0f, 1.0f, 0.0f ), 1.0f, 1.0f );
	
	transform.setOrigin( btVector3( hworldX, 50, -hwallwidth ) );
	body.addBodyPart_Rigid_Box( btVector3( hworldX+wallwidth, 50, hwallwidth ), 0, localInertia, transform, BeColor( 0.5f, 1.0f, 1.0f, 0.0f ), 1.0f, 1.0f );

	transform.setOrigin( btVector3( hworldX, 50, worldZ+hwallwidth ) );
	body.addBodyPart_Rigid_Box( btVector3( hworldX+wallwidth, 50, hwallwidth ), 0, localInertia, transform, BeColor( 0.5f, 1.0f, 1.0f, 0.0f ), 1.0f, 1.0f );

	// MEASURING CUBES
/*	transform.setOrigin( btVector3( 1000, relativeY-hwallwidth, 1000 ) );
	body.addBodyPart_Rigid_Box( btVector3( 1000, hwallwidth, 1000 ), 0, localInertia, transform, Color( 0.5f, 1.0f, 1.0f, 0.0f ), 1.0f, 1.0f );
	transform.setOrigin( btVector3( 1000, -hwallwidth, 1000 ) );
	body.addBodyPart_Rigid_Box( btVector3( 1000, hwallwidth, 1000 ), 0, localInertia, transform, Color( 0.5f, 1.0f, 1.0f, 0.0f ), 1.0f, 1.0f );*/
}

void TileMap::fillSector( unsigned int floor, unsigned int x, unsigned int z, const string& object, unsigned int orientation  )
{
	btTransform orientationtr; orientationtr.setIdentity();
	orientationtr.setOrigin( btVector3( 0,0,0 ) );
	orientationtr.getBasis().setEulerZYX( 0.0, -1.57079632679 * orientation, 0.0 ); // -1.5707f = 90 degrees

	btTransform sectorPos; sectorPos.setIdentity();
	sectorPos.setOrigin( getSectorVector(x, z, (floor*relativeY)+(relativeY)/2 ) );
	sectorPos *= orientationtr;

	btTransform poffset; 
	poffset.setIdentity();

	btVector3 inert(0,0,0);
// STARGRID
	if ( object == "startgrid" )
	{
		// 2 walls
		poffset.setOrigin( btVector3( 0, -((floor*relativeY)+(relativeY-wallwidth)/2)+350, 0 ) );
		poffset.getBasis().setEulerZYX( 0.0f, 3.14159265359, 0.0f ); // -1.5707f = 90 degrees
		startgrid = sectorPos*poffset;
	}

// FINISHLINE
	else if ( object == "finish" )
	{
		finish = sectorPos;

		// 2 upstanding cubes
		poffset.setOrigin( btVector3( -hsectorsize+wallwidth, 0, 0 )  );
		body.addBodyPart_Rigid_Box( btVector3( hwallwidth, (relativeY-wallwidth)/2, hwallwidth ), 0, inert, sectorPos*poffset, BeColor( 0.3f, 0.3f, 0.5f, 0.0f ), 0.05, 0.85 );

		poffset.setOrigin( btVector3( hsectorsize-wallwidth, 0, 0 )  );
		body.addBodyPart_Rigid_Box( btVector3( hwallwidth, (relativeY-wallwidth)/2, hwallwidth ), 0, inert, sectorPos*poffset, BeColor( 0.3f, 0.3f, 0.5f, 0.0f ), 0.05, 0.85 );
	}

// SET A : sided by columns

	else if ( object == "road_B_roof" )
	{
		fillSector( floor, x, z, "road_B", orientation  );
		fillSector( floor, x, z, "topplate_A", orientation  );
	}

	else if ( object == "road_B_h_roof" )
	{
		// 1 walls
		fillSector( floor, x, z, "road_B_h", orientation  );
		fillSector( floor, x, z, "topplate_A", orientation  );
	}
	else if ( object == "corner_B_roof" )
	{
		fillSector( floor, x, z, "corner_B", orientation  );
		fillSector( floor, x, z, "topplate_A", orientation  );
	}

// 	else
// 	{
// 		string fullmodulefilename("share/stuntcoureur/modules/");
// 		fullmodulefilename.append(object);
// 		fullmodulefilename.append(".obj");
// 
// 		poffset.setOrigin( btVector3( 0, -hrelativeY, 0 )  );
// 		OBJloader k(fullmodulefilename);
// // 		BodypartRigidTrianglemesh* b = body.addBodyPart_Rigid_Trianglemesh(
// // 			&k, 
// // 			localScale, 0, inert, sectorPos*poffset, Color( 1.0f, 1.0f, 1.0f, 1.0f ), 0, 0 );
// 		body.addBodyPart_Rigid_Trianglemesh(
// 			&k, 
// 			localScale, 0, inert, sectorPos*poffset, Color( 1.0f, 1.0f, 1.0f, 1.0f ), 0, 0 );
// 	}

// CLIMB

// 	else { cerr << object << " is not a recognized object type" << endl; }
}

/*void TileMap::insertCyl(const btTransform& pos)
{
	btVector3 inert(0,0,0);
// 	BodypartRigid* b = body.addBodyPart_Rigid_Cylinder( btVector3( (sectorsize/13), (relativeY-wallwidth)/2, 8), 0, inert, pos, Color( 0.7109375f, 0.7109375f, 0.53515625f, 1.0f ), 0.15f, 0.95f );
	body.addBodyPart_Rigid_Cylinder( btVector3( (sectorsize/13), (relativeY-wallwidth)/2, 8), 0, inert, pos, BeColor( 0.7109375f, 0.7109375f, 0.53515625f, 1.0f ), 0.15f, 0.95f );
}*/

btVector3 TileMap::getSectorVector(unsigned int x, unsigned int z, btScalar height)
{
	return btVector3( (sectorsize * x)+hsectorsize, height, (sectorsize * z)+hsectorsize );
}



void TileMap::load(const char* filename)
{
	cerr << "loading map '" << filename << "'" << endl;

// 	string content;
// 	if ( fileH.open( filename, content ) )
// 	{
// 		content.append("\n");
// 		string line = parseH.returnUntillStrip( "\n", content );
// 
// 		while ( !content.empty() )
// 		{
// 			// trim spaces
// 			while ( parseH.beginMatchesStrip( " ", line ) ) {};
// 			while ( parseH.beginMatchesStrip( "\t", line ) ) {};
// 			// remove comment lines
// 			if ( parseH.beginMatchesStrip( "//", line ) ) line.clear();
// 			if ( parseH.beginMatchesStrip( "#", line ) ) line.clear();
// 
// 			if ( line.length() > 1 )
// 			{
// 				line.append(" ");
// 
// 				unsigned int fl(0), x(0), y(0), ori(0);
// 				string object;
// 
// 				string fls = parseH.returnUntillStrip( " ", line );
// 				if ( !fls.empty() ) fl = atoi( fls.c_str() );
// 
// 				// x
// 				while ( parseH.beginMatchesStrip( " ", line ) ) {};
// 				while ( parseH.beginMatchesStrip( "\t", line ) ) {};
// 				string xs = parseH.returnUntillStrip( " ", line );
// 				if ( !xs.empty() ) x = atoi( xs.c_str() );
// 
// 				// y
// 				while ( parseH.beginMatchesStrip( " ", line ) ) {};
// 				while ( parseH.beginMatchesStrip( "\t", line ) ) {};
// 				string ys = parseH.returnUntillStrip( " ", line );
// 				if ( !ys.empty() ) y = atoi( ys.c_str() );
// 
// 				// object
// 				while ( parseH.beginMatchesStrip( " ", line ) ) {};
// 				while ( parseH.beginMatchesStrip( "\t", line ) ) {};
// 				object = parseH.returnUntillStrip( " ", line );
// 
// 				// orientation
// 				while ( parseH.beginMatchesStrip( " ", line ) ) {};
// 				while ( parseH.beginMatchesStrip( "\t", line ) ) {};
// 				string oris = parseH.returnUntillStrip( " ", line );
// 				if ( !oris.empty() ) ori = atoi( oris.c_str() );
// 
// // 				cerr << "loading floor : " << fl << endl;
// // 				cerr << "x : " << x << endl;
// // 				cerr << "y : " << y << endl;
// // 				cerr << "object : " << object << endl;
// // 				cerr << "ori : " << ori << endl;
// 
// 				fillSector( fl, x, y, object, ori );
// 			}
// 
// 
// 			line = parseH.returnUntillStrip( "\n", content );
// 		}
// 	}
}

TileMap::~TileMap()
{
}


